package easy

import "strings"

/*
时间复杂度:O(N+M) pattern长度为N, s长度为M
空间复杂度:O(N+M)
*/

func wordPattern(pattern string, s string) bool {
	sArr := strings.Split(s, " ")
	if len(pattern) != len(sArr) {
		return false
	}
	ch2word := make(map[byte]string)
	word2ch := make(map[string]byte)
	for i := range pattern {
		ch := pattern[i]
		if (ch2word[ch] != "" && ch2word[ch] != sArr[i]) || (word2ch[sArr[i]] > 0 && word2ch[sArr[i]] != ch) {
			return false
		}
		ch2word[ch] = sArr[i]
		word2ch[sArr[i]] = ch
	}
	return true
}